package project.service.impl;

import project.dao.IPetDao;
import project.dao.IUserDao;
import project.entity.Pet;
import project.entity.User;
import project.impl.PetDaoImpl;
import project.impl.UserDaoImpl;
import project.service.IPetService;
import project.service.IUserService;
import project.ui.Menu;
import project.util.Constants;

public class PetShop implements IUserService, IPetService {

	private IUserDao userDao;
	private IPetDao petDao;

	public PetShop() {
		userDao = new UserDaoImpl();
		petDao = new PetDaoImpl();
	}

	public void process() {
		boolean f = true;

		while (f) {
			if (login()) {
				boolean isContinue = true;
				while (isContinue) {
					String opt = Menu.mainMenu();
					switch (opt) {
					case Constants.ADD_PET:
						addPet();
						break;
					case Constants.DEL_PET:
						delPet();
						break;
					case Constants.UPDATE_PET:
						updatePrice();
						break;
					case Constants.FINDALL_PET:
						findAllPets();
						break;
					case Constants.FIND_ONE_PET:
						findById();
						break;
					case Constants.EXIT:
						isContinue = false;
						break;
					}
				}

			}
			return;
		}
		
	}

	@Override
	public void addPet() {
		Pet pet = Menu.addPetMenu();
		petDao.save(pet);
	}

	@Override
	public void delPet() {
		int id = Menu.delPetMenu();
		petDao.delete(id);
	}

	@Override
	public void updatePrice() {
		int id = Menu.delPetMenu();
		double newPrice = Menu.updatePriceMenu();
		petDao.updatePrice(id, newPrice);

	}

	@Override
	public void findAllPets() {
		System.out.println(petDao.findAll());
	}

	@Override
	public void findById() {
		int id = Menu.delPetMenu();
		System.out.println(petDao.findById(id));
	}

	@Override
	public boolean login() {
		User user = Menu.loginMenu();
		return userDao.findByUsernameAndPassword(user.getUsername(), user.getPassword()) != null;
	}
	public static void main(String[] args) {
		PetShop ps = new PetShop();	
		ps.process();
	}

}
